﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Security.Cryptography.X509Certificates;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace iNFeDll
{
    public partial class frmConsulta : Form
    {
        public frmConsulta()
        {
            InitializeComponent();
        }

        private string RetornoSefaz(object rets, string pegarTag, string motivo)
        {

            var xDoc = XElement.Parse((rets as XmlElement).OuterXml);
            string status = xDoc.Descendants().Where(r => r.Name.LocalName.Equals("cStat")).FirstOrDefault().Value.ToString();
            if (status != "100")
            {
                status = "Status: " + xDoc.Descendants().Where(r => r.Name.LocalName.Equals("cStat")).FirstOrDefault().Value.ToString() + "\n" +
                                "Motivo: " + xDoc.Descendants().Where(r => r.Name.LocalName.Equals("xMotivo")).FirstOrDefault().Value.ToString(); ;
            }
            else
            {
                XmlDocument doc_ = new XmlDocument();
                doc_.LoadXml((rets as XmlElement).OuterXml);
                XmlNodeList retRetNFeList = doc_.GetElementsByTagName("protNFe");
                foreach (XmlNode retRetNFeNode in retRetNFeList)
                {
                    XmlElement retCancNFeElemento = (XmlElement)retRetNFeNode;
                    XmlNodeList infRetcList = retCancNFeElemento.GetElementsByTagName("infProt");

                    foreach (XmlNode infRetcNode in infRetcList)
                    {
                        XmlElement infRetElemento = (XmlElement)infRetcNode;
                        if (infRetElemento.GetElementsByTagName("cStat")[0].InnerText.ToString().Equals("100"))
                        {
                            status = "Status: " +
                                infRetElemento.GetElementsByTagName("cStat")[0].InnerText + "\n" +
                                "Motivo: " + infRetElemento.GetElementsByTagName("nProt")[0].InnerText + "\n" +
                                "Data: " + infRetElemento.GetElementsByTagName("dhRecbto")[0].InnerText + "\n" +
                                "Motivo: " + infRetElemento.GetElementsByTagName("xMotivo")[0].InnerText;
                        }
                    }
                }
            }
            return status;
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            string status = string.Empty;
            lblConsulta.Visible = true;
            lblConsulta.Refresh();
            try
            {
                Integra_ERP.ClasseConsultaNFe clsCon = new Integra_ERP.ClasseConsultaNFe();
                var retorno = clsCon.ExecutaConsulta(txtChaveDeAcesso.Text);
                txtResposta.Text = RetornoSefaz(retorno,"cStat","xMotivo");
                lblConsulta.Visible = false;
            }
            catch (ExecutionEngineException ex)
            {
                txtResposta.Text = ex.Message;
                lblConsulta.Visible = false;
            }
            
        }


      

       
    }
}
